﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;

namespace ESOL.WinUI.Hr
{
    public partial class frmManageAttendance : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        #endregion

        #region Constructor
        public frmManageAttendance()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetField()
        {
            dtpYm.EditValue = DateTime.Today;
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND ACL.DEPARTMENT_ID = '" + department.DepartmentId + "'";
            }
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                condition += " AND ACL.PERSON_ID = '" + person.PersonId + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            char c = Convert.ToChar("0");
            string date = Convert.ToDateTime(dtpYm.EditValue).ToString("yyyy-MM");
            string orderBy = "PERSON_CODE";
            DataTable dt = HrAttendanceRecordBs.GetOverviewByCondition(condition, orderBy, date);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            SetField();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPerson = new MstPersonEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            char c = Convert.ToChar("0");
            int year = (Convert.ToDateTime(dtpYm.EditValue)).Year;
            int month = (Convert.ToDateTime(dtpYm.EditValue)).Month;
            string date = year.ToString() + "-" + month.ToString().PadLeft(2, c);

            frmMaintainAttendanceRecordQuery frm = new frmMaintainAttendanceRecordQuery(mSelectedPerson, date);
            frm.ViewOnly = true;
            frm.ShowDialog();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        protected override void Click_Import()
        {
            frmMaintainAttendanceImport frm = new frmMaintainAttendanceImport();
            if(frm.ShowDialog()==DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        #endregion

        private void btnExport_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(sfd.FileName);
            }
        }
    }
}
